package hflyeer.gitee.com.controller;

import hflyeer.gitee.com.entity.Employee;
import hflyeer.gitee.com.global.Constant;
import hflyeer.gitee.com.service.DepartmentService;
import hflyeer.gitee.com.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;


/**
 * 员工控制器
 */
@Controller("employeeController")
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private DepartmentService departmentService;

    @GetMapping("/list")
    public String list(Map<String,Object> map){
        map.put("employeeList",employeeService.getAll());
        return "employee_list";
    }

    @GetMapping("/to_add")
    public String toAdd(Map<String,Object> map){
        map.put("employee",new Employee());
        map.put("department",departmentService.getAll());
        map.put("posts", Constant.getPosts());
        return "employee_add";
    }
    @PostMapping("/add")
    public String add(Employee employee){
        employeeService.insert(employee);
        return "redirect:list";
    }

    @GetMapping(value = "/to_update",params = "sn")
    public String toEdit(String sn,Map<String,Object> map){
        map.put("employee",employeeService.get(sn));
        map.put("department",departmentService.getAll());
        map.put("posts", Constant.getPosts());
        return "employee_update";
    }
    @PostMapping("/update")
    public String edit(Employee employee){
        employeeService.update(employee);
        return "redirect:list";
    }

    @GetMapping(value = "/remove",params = "sn")
    public String remove(String sn){
        employeeService.delete(sn);
        return "redirect:list";
    }
}
